package cn.madis.ymcc.mq.consumer;

import cn.madis.ymcc.service.ICourseOrderService;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;


@Component
@RocketMQMessageListener(
        consumerGroup = "paytimeout-order-consumer",//组名
        topic = "timeoutcancel-order-topic",
        selectorExpression = "paytimeout-tags",//tags
        messageModel = MessageModel.BROADCASTING//广播模式
        // 集群模式一个消费者只能给到一个消费者

)
public class PayTimeOutCancelOrderConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;


    /**
     * 消费者只能成功消费一次
     * 这个方法只要不报错就自动签收
     * @param messageExt
     */
    @Override
    public void onMessage(MessageExt messageExt) {
        byte[] body = messageExt.getBody();
        String orderNo = new String(body, StandardCharsets.UTF_8);
        if (StringUtils.isEmpty(orderNo)){
            return;
        }
        /*支付超时取消订单逻辑*/
        courseOrderService.payTimeOutCancelOrder(orderNo);

    }
}

